package com.lagou.work1;

import java.io.*;
import java.util.*;

public class StudentOper {

    private List<Student> dataList = new ArrayList<>();

    public void initStudent() {
        ObjectInputStream ois = null;

        try {
            // 1.创建ObjectInputStream类型的对象与d:/a.txt文件关联
            FileInputStream fileInputStream = new FileInputStream("/Users/dingxiaowei/Downloads/a.txt");
            if (fileInputStream.available() > 0) {
                ois = new ObjectInputStream(fileInputStream);
                // 2.从输入流中读取一个对象并打印
                Object obj = ois.readObject();
                dataList = (List<Student>) obj;
                System.out.println("读取到的对象是：" + obj); //
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            // 3.关闭流对象并释放有关的资源
            if (null != ois) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void operStuMsg1(Scanner scanner) {
        System.out.println("------------------------");
        System.out.println("请根据序号选择以下功能");
        System.out.println("1.添加学生信息");
        System.out.println("2.删除学生信息");
        System.out.println("3.修改学生信息");
        System.out.println("4.查找学生信息");
        System.out.println("5.查看所有学生信息");
        System.out.println("6.退出系统");
        System.out.println("请输入序号");
        int str = scanner.nextInt();
        if (str == 6) {
            ObjectOutputStream oos = null;
            try {
                // 1.创建ObjectOutputStream类型的对象与d:/a.txt文件关联
                oos = new ObjectOutputStream(new FileOutputStream("/Users/dingxiaowei/Downloads/a.txt"));
                // 2.将整个dataList类型的对象写入输出流
                oos.writeObject(dataList);
                System.out.println("写入对象成功！");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 4.关闭流对象并释放有关的资源
                if (null != oos) {
                    try {
                        oos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return;
        }
        switch (str) {
            case 1:
                System.out.println("请输入学生学号、姓名、年龄(以空格进行分割)");
                int num = scanner.nextInt();
                String name = scanner.next();
                int age = scanner.nextInt();
                Student student = null;
                try {
                    student = new Student(num, name, age);
                    StudentAdd.addStudent(student, dataList);
                } catch (AgeException e) {
                    e.printStackTrace();
                } catch (StudentNumException e) {
                    e.printStackTrace();
                }
                break;
            case 2:
                System.out.println("请输入学生学号进行删除");
                num = scanner.nextInt();
                try {
                    StudentDel.delStudent(num, dataList);
                } catch (StudentNumException e) {
                    e.printStackTrace();
                }
                break;
            case 3:
                System.out.println("请输入学生学号、姓名、年龄进行修改(以空格进行分割)");
                num = scanner.nextInt();
                name = scanner.next();
                age = scanner.nextInt();
                try {
                    StudentUpdate.updateStudent(num, name, age, dataList);
                } catch (StudentNumException e) {
                    e.printStackTrace();
                } catch (AgeException e) {
                    e.printStackTrace();
                }
                break;
            case 4:
                System.out.println("请输入学生学号进行查询");
                num = scanner.nextInt();
                try {
                    StudentFind.findStudent(num, dataList);
                } catch (StudentNumException e) {
                    e.printStackTrace();
                }
                break;
            case 5:
                System.out.println("所有学生的信息为：");
                StudentFind.showStudent(dataList);
                break;
            default:
                System.out.println("无此选项请重新输入！");
                break;
        }
        this.operStuMsg1(scanner);
    }

}
